import { ref, unref } from "vue";

export function useModal(props) {
  const instanceRef = ref();
  const uuidRef = ref();

  function register(dialogInstance, uuid) {
    instanceRef.value = dialogInstance;
    uuidRef.value = uuid;

    props && methods.setProps(props);
  }

  const getInstance = () => {
    const instance = unref(instanceRef);
    if (!instance) {
      console.error("useModal instance is undefined!");
    }
    return instance;
  };

  const methods = {
    actionDialog(open = true) {
      getInstance()?.actionDialog(open);
    },
    setProps(props) {
      getInstance()?.setProps(props);
    },
  };

  return [register, methods];
}
